<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */
class UserController extends HomeController {
	/* 注册页面 */
	public function register(){
        if(IS_POST){
            $user=D('user');
          
            //dump($res);die; 
            $img = $this->upload(); 
            $img=$img["img"];
            $img='/uploads/'.$img['savepath'].$img['savename'];
            $post=I('post.');
            $post['img']=$img;
            //dump($post);
            $res=$user->create($post);  
            $yzm = $this->check_verify(I('post.verify'));
            if(!$yzm){
                $this->error('验证码输入错误',U('User/register'));
            }else{

            }
            //dump($img);die;
            if($res){
                if($user->add($res)){
                    import('Org.Net.Mail');
                    $address = $res['email'];
                    $title = '账号激活';
                    $content = 'shop.com'.U('doactive',array('email'=>$address));
                    SendMail($address, $title, $content);
                    $this->success('注册成功,前往邮箱激活账号即可正常使用',U('User/login'));
                    
                }else{
                   $this->error('注册失败',U('User/register')); 
                }  
            }else{  
               $this->error('注册失败'.$user->getError(),U('User/register'));
            }
        }else{
           $this->display();
        }   
	}


    public function upload(){
        //dump($_FILES["img"]);die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './upload/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
    
        // 上传文件 
        $info   =   $upload->upload();
        //dump($info);die;
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            //$this->success('上传成功！');      
            return $info;
        }
    }



    //账号激活
    public function doactive(){
        $email = I('get.email');
        $user = D('user');
        $res = $user->where(array('email'=>$email))->setField('active',1);
        if($res){
            $this->success('激活成功！',U('User/login'));
        }
    }

	/* 登录页面 */
	public function login(){
        if(IS_POST){
            $user=D('user');
            //dump(I('post.'));die;
            $where=I('post.');
            $where['pwd']=I('post.pwd'); // md5(I('post.pwd'))加密；
            $res=$user->where($where)->find();  
            //dump($res);die;
            //$ass=$user->where(I('post.'));
               $c= I('post.email');
                if($res){
                    if($res['active'] !=1){
                        $this->error('请先激活账号');
                    }
                        session('email',$c); 
                        session('img',$res['img']);
                        $this->success('登录成功',U('Index/index')); 
                    
                }else{
                     $this->error('email或密码错误'.$user->getError(),U('User/login'));
                }  
            
        }else{
            $this->display();
        }   
    }


	/* 退出登录 */
	public function logout(){
		if(is_login()){
			D('user')->logout();
			$this->success('退出成功！', U('User/login'));
		} else {
			$this->redirect('User/login');
		}
	}


	/* 验证码，用于登录和注册 */
	public function verify(){
        $config = array(
            'fontSize' => 25, // 验证码字体大小
            'length' => 3, // 验证码位数
            'useNoise' => true, // 关闭验证码杂点
        );
		$Verify = new \Think\Verify($config);
         $Verify->entry();
	}


    public function check_verify($code, $id = ''){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
    }

	/**
	 * 获取用户注册错误信息
	 * @param  integer $code 错误编码
	 * @return string        错误信息
	 */
	private function showRegError($code = 0){
		switch ($code) {
			case -1:  $error = '用户名长度必须在16个字符以内！'; break;
			case -2:  $error = '用户名被禁止注册！'; break;
			case -3:  $error = '用户名被占用！'; break;
			case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
			case -5:  $error = '邮箱格式不正确！'; break;
			case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
			case -7:  $error = '邮箱被禁止注册！'; break;
			case -8:  $error = '邮箱被占用！'; break;
			case -9:  $error = '手机格式不正确！'; break;
			case -10: $error = '手机被禁止注册！'; break;
			case -11: $error = '手机号被占用！'; break;
			default:  $error = '未知错误';
		}
		return $error;
	}


    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function profile(){
		if ( !is_login() ) {
			$this->error( '您还没有登陆',U('User/login') );
		}
        if ( IS_POST ) {
            //获取参数
            $uid        =   is_login();
            $pwd   =   I('post.old');
            $repassword = I('post.repwd');
            $data['pwd'] = I('post.pwd');
            empty($pwd) && $this->error('请输入原密码');
            empty($data['pwd']) && $this->error('请输入新密码');
            empty($repwd) && $this->error('请输入确认密码');

            if($data['pwd'] !== $repwd){
                $this->error('您输入的新密码与确认密码不一致');
            }

            $Api = new UserApi();
            $res = $Api->updateInfo($uid, $pwd, $data);
            if($res['status']){
                $this->success('修改密码成功！');
            }else{
                $this->error($res['info']);
            }
        }else{
            $this->display();
        }
    }

}
